package com.utils;

/**
 * @Auther: djianfeng
 * @Date: 2018/8/27 10:02
 * @Description:
 */
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateFormatUtil {
    public DateFormatUtil() {
    }

    public static Date getDate(String format) {
        if (StringUtils.isBlank(format)) {
            format = "yyyy-MM-dd";
        }

        SimpleDateFormat f = new SimpleDateFormat(format);

        try {
            return f.parse(f.format(new Date()));
        } catch (ParseException var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static Date getDateYMDHMS() {
        return getDate("yyyy-MM-dd HH:mm:ss");
    }

    public static Date getDateYMD() {
        return getDate("yyyy-MM-dd");
    }

    public static Date getDateYM() {
        return getDate("yyyy-MM");
    }

    public static Date getDateY() {
        return getDate("yyyy");
    }

    public static String getStringDate(String format) {
        if (StringUtils.isBlank(format)) {
            format = "yyyy-MM-dd HH:mm:ss";
        }

        SimpleDateFormat f = new SimpleDateFormat(format);

        try {
            return f.format(new Date());
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static String getStringDateYMDHMS() {
        return getStringDate((String) null);
    }

    public static String getStringDateYMD() {
        return getStringDate("yyyy-MM-dd");
    }

    public static String getStringDateYM() {
        return getStringDate("yyyy-MM");
    }

    public static String formatDate(Date date, String format) {
        if (date == null) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        }
    }

    public static String formatDateYMDHMS(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String formatDateYMD(Date date) {
        return formatDate(date, "yyyy-MM-dd");
    }

    public static String formatDateYM(Date date) {
        return formatDate(date, "yyyy-MM");
    }

    public static String formatDateY(Date date) {
        return formatDate(date, "yyyy");
    }

    public static String formatStringDate(String date, String format) {
        if (StringUtils.isNotBlank(date)) {
            try {
                String dateformat = format.replace("年", "-").replace("月", "-").replace("日", "");
                SimpleDateFormat sdf = new SimpleDateFormat(dateformat);
                Date d = sdf.parse(date);
                sdf = new SimpleDateFormat(format);
                return sdf.format(d);
            } catch (ParseException var5) {
                var5.printStackTrace();
            }
        }

        return null;
    }

    public static String formatStringDateYMDHMS(String date) {
        return formatStringDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String formatStringDateYMDHM(String date) {
        return formatStringDate(date, "yyyy-MM-dd HH:mm");
    }

    public static String formatStringDateYMD(String date) {
        return formatStringDate(date, "yyyy-MM-dd");
    }

    public static String formatStringDateYM(String date) {
        return formatStringDate(date, "yyyy-MM");
    }

    public static String formatStringDateY(String date) {
        return formatStringDate(date, "yyyy");
    }

    public static Date convertDate(String value, String format) {
        if (StringUtils.isBlank(format)) {
            format = "yyyy-MM-dd";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        if (StringUtils.isNotBlank(value)) {
            try {
                return sdf.parse(value.toString());
            } catch (ParseException var4) {
                var4.printStackTrace();
            }
        }

        return null;
    }

    public static Date convertDateYMDHMS(String value) {
        return convertDate(value, "yyyy-MM-dd HH:mm:ss");
    }

    public static Date convertDateYMD(String value) {
        return convertDate(value, "yyyy-MM-dd");
    }

    public static Date convertDateYM(String value) {
        return convertDate(value, "yyyy-MM");
    }

    public static Date convertDateY(String value) {
        return convertDate(value, "yyyy");
    }

    public static Date convertDateToDate(Date date, String format) {
        if (StringUtils.isBlank(format)) {
            format = "yyyy-MM-dd";
        }

        if (date != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                return sdf.parse(sdf.format(date));
            } catch (ParseException var3) {
                var3.printStackTrace();
            }
        }

        return null;
    }

    public static Date convertDateToDateYMDHMS(Date date) {
        return convertDateToDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static Date convertDateToDateYMD(Date date) {
        return convertDateToDate(date, "yyyy-MM-dd");
    }

    public static Date convertDateToDateYM(Date date) {
        return convertDateToDate(date, "yyyy-MM");
    }

    public static Date convertDateToDateY(Date date) {
        return convertDateToDate(date, "yyyy");
    }

    public static long getDifferDay(Date beginDate, Date endDate) {
        long day = (beginDate.getTime() - endDate.getTime()) / 86400000L > 0L ? (beginDate.getTime() - endDate.getTime()) / 86400000L : (endDate.getTime() - beginDate.getTime()) / 86400000L;
        return day;
    }


    public static String getYear() {
        return getIntYear() + "";
    }

    public static Integer getIntYear() {
        Calendar cal = Calendar.getInstance();
        return cal.get(1);
    }

    public static String getYear(Date date) {
        return date != null ? getIntYear() + "" : null;
    }

    public static Integer getIntYear(Date date) {
        if (date != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            return cal.get(1);
        } else {
            return null;
        }
    }

    public static Integer getIntYear(String date) {
        if (date != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(convertDateYMD(date));
            return cal.get(1);
        } else {
            return null;
        }
    }

    public static Integer getDayOfMonth(Date date) {
        return getTime(date, 5);
    }

    public static Integer getHourOfDay(Date date) {
        return getTime(date, 11);
    }

    public static Integer getMonth(Date date) {
        return getTime(date, 2) + 1;
    }

    public static Integer getTime(Date date, int timeType) {
        int rst = -1;
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            rst = calendar.get(timeType);
        }

        return rst;
    }

    public static Date decreaseDateByHour(Date date, int hour) {
        int dayHour = getHourOfDay(date) - hour;
        return formatDateSetHour(date, dayHour);
    }

    public static Date decreaseDateByDay(Date date, int day) {
        int dayOfMonth = getDayOfMonth(date) - day;
        return formatDateSetDay(date, dayOfMonth);
    }

    public static Date decreaseDateByMonth(Date date, int month) {
        int gMonth = getMonth(date) - month;
        return formatDateSetMonth(date, gMonth);
    }

    public static Date formatDateSetMonth(Date date, int month) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(2, month - 1);
            return calendar.getTime();
        } else {
            return null;
        }
    }

    public static Date formatDateSetYear(Date date, int year) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(1, year);
            return calendar.getTime();
        } else {
            return null;
        }
    }

    public static Date formatDateSetDay(Date date, int day) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(5, day);
            return calendar.getTime();
        } else {
            return null;
        }
    }

    public static Date formatDateSetHour(Date date, int hour) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(11, hour);
            return calendar.getTime();
        } else {
            return null;
        }
    }

    public static Date add(Date date, int field, int amount) {
        if (date != null) {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(field, amount);
            return c.getTime();
        } else {
            return null;
        }
    }

    public static Date addYear(Date date, int amount) {
        return add(date, 1, amount);
    }

    public static Date addMonth(Date date, int amount) {
        return add(date, 2, amount);
    }

    public static Date addDay(Date date, int amount) {
        return add(date, 5, amount);
    }

    public static Date addHour(Date date, int amount) {
        return add(date, 11, amount);
    }

    public static Date addMinute(Date date, int amount) {
        return add(date, 12, amount);
    }

    public static Date addSecond(Date date, int amount) {
        return add(date, 13, amount);
    }

    public static String addDay(String date, int n) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(format.parse(date));
            c.add(5, n);
            return format.format(c.getTime());
        } catch (Exception var4) {
            var4.printStackTrace();
            return null;
        }
    }

    public static String[] getMondayAndSunday(String str) {
        String[] result = new String[2];
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date date = sDateFormat.parse(str);
            calendar.setTime(date);
            int flag = calendar.get(7);
            calendar.add(6, -flag + 2);
            date = calendar.getTime();
            result[0] = sDateFormat.format(date);
            calendar.add(6, 6);
            date = calendar.getTime();
            result[1] = sDateFormat.format(date);
        } catch (ParseException var6) {
            var6.printStackTrace();
        }

        return result;
    }

    public String[] getWeeks(String str) {
        String[] result = new String[7];
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date date = sDateFormat.parse(str);
            calendar.setTime(date);
            int flag = calendar.get(7);
            calendar.add(6, -flag + 2);
            date = calendar.getTime();
            result[0] = sDateFormat.format(date);

            for (int i = 0; i < 6; ++i) {
                calendar.add(6, 1);
                date = calendar.getTime();
                result[i + 1] = sDateFormat.format(date);
            }
        } catch (ParseException var8) {
            var8.printStackTrace();
        }

        return result;
    }

    public static Integer getDayOfWeek(String datestr) {
        Integer result = 1;
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date date = sDateFormat.parse(datestr);
            calendar.setTime(date);
            int flag = calendar.get(7);
            if (flag == 1) {
                flag = 8;
            }

            result = flag - 1;
        } catch (ParseException var6) {
            var6.printStackTrace();
        }

        return result;
    }

    public static String getDayOfWeekStr(String datestr) {
        String[] weeks = new String[]{" ", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
        Integer week = getDayOfWeek(datestr);
        return weeks[week];
    }

    public static Integer getWeekOfYear(String datestr) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = format.parse(datestr);
            Calendar calendar = Calendar.getInstance();
            calendar.setFirstDayOfWeek(2);
            calendar.setTime(date);
            return calendar.get(3);
        } catch (ParseException var4) {
            var4.printStackTrace();
            return null;
        }
    }

    public static String[] nextMonday() throws ParseException {
        String[] str = new String[2];
        Date ks = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String riqi = "";
        if (ks.getDay() != 0) {
            riqi = formatter.format(new Date(ks.getTime() + 604800000L));
        } else {
            riqi = formatter.format(ks);
        }

        Calendar as = Calendar.getInstance();
        as.setTime(formatter.parse(riqi));
        as.set(7, 2);
        str[0] = formatter.format(as.getTime());
        str[1] = formatter.format(new Date(as.getTime().getTime() + 518400000L));
        return str;
    }

    public static Map<String, String> getBetweenMonth(Date begin, Date end) {
        Map<String, String> dayMap = new LinkedHashMap();
        GregorianCalendar gc1 = new GregorianCalendar();
        GregorianCalendar gc2 = new GregorianCalendar();
        gc1.setTime(begin);
        gc2.setTime(end);

        do {
            GregorianCalendar gc3 = (GregorianCalendar) gc1.clone();
            String key = gc3.get(1) + "-";
            String value = gc3.get(1) + "年";
            Integer month = gc3.get(2) + 1;
            if (month < 10) {
                key = key + "0" + month;
                value = value + "0" + month + "月";
            } else {
                key = key + month;
                value = value + month + "月";
            }

            dayMap.put(key, value);
            gc1.add(2, 1);
        } while (!gc1.after(gc2));

        return dayMap;
    }

    public static String getBetweenDate(Date begin, Date end) {
        StringBuffer dates = new StringBuffer();
        GregorianCalendar gc1 = new GregorianCalendar();
        GregorianCalendar gc2 = new GregorianCalendar();
        gc1.setTime(begin);
        gc2.setTime(end);

        do {
            GregorianCalendar gc3 = (GregorianCalendar) gc1.clone();
            if (dates.length() > 0) {
                dates.append(",");
            }

            dates.append(formatDateYMD(gc3.getTime()));
            gc1.add(5, 1);
        } while (!gc1.after(gc2));

        return dates.toString();
    }

    public static String getBetweenDateEx(String begin, String end) {
        Date beginEx = convertDateYMD(begin);
        Date endEx = convertDateYMD(end);
        StringBuffer dates = new StringBuffer();
        GregorianCalendar gc1 = new GregorianCalendar();
        GregorianCalendar gc2 = new GregorianCalendar();
        gc1.setTime(beginEx);
        gc2.setTime(endEx);

        do {
            GregorianCalendar gc3 = (GregorianCalendar) gc1.clone();
            if (dates.length() > 0) {
                dates.append(",");
            }

            dates.append(formatDateYMD(gc3.getTime()));
            gc1.add(5, 1);
        } while (!gc1.after(gc2));

        return dates.toString();
    }

    public static boolean isValidDate(String str, String format) {
        if (StringUtils.isBlank(str)) {
            return false;
        } else {
            if (StringUtils.isBlank(format)) {
                format = "yyyy-MM-dd";
            }

            boolean convertSuccess = true;

            try {
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                sdf.setLenient(false);
                sdf.parse(str);
            } catch (ParseException var4) {
                convertSuccess = false;
            }

            return convertSuccess;
        }
    }

    public static void main(String[] args) {
        String d = "2017-11-04 12:23:45";
        String dd = addDay((String) d, 7);
        System.out.println(d);
        System.out.println(dd);
        long c = getDifferDay(getDateYMD(), convertDateYMD(dd));
        System.out.println(c);
        long day = (getDateYMD().getTime() - convertDateYMD(dd).getTime()) / 86400000L;
        System.out.println(day);
    }
}
